package com.gexin.model.product;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisKeyConst;
import com.gexin.common.model.AjaxCmd;
import com.gexin.model.product.base.BaseProductTag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务名称: t_product_tag(t_product_tag)
 */
@SuppressWarnings("serial")
public class ProductTag extends BaseProductTag<ProductTag> {
	public static final ProductTag dao = new ProductTag();
	private static final String key_index = "cache:tag:index";
	/*缓存专题下子类*/
	public static final String key_Tag_List = "cache:tag:list:";
	/*缓存专题下面的商品*/
	public static final String ProductCache_key="cache:tag:product:";


	// 专题
	public AjaxCmd tags() {
		List<ProductTag> list = ProductTag.dao.find("select * from t_product_tag t where t.pid=0 order by t.tag_sort desc");
		for (ProductTag t : list) {
			t.put("list", ProductTag.dao.find("select t.* from t_product_tag t where t.pid = ? order by t.tag_sort desc", t.getTagId()));
			t.put("products", new ArrayList<Products>());
		}
		return new AjaxCmd(list);
	}

	/**
	 * 从缓存里面读取 商品标签 缓存周期一天
	 * @return
	 */
	public List<ProductTag> cache_GetIndexTag() {
		List<ProductTag> list = CacheUtils.get(key_index);
		if (list == null || list.isEmpty()) {
			list = find("select  tag_icon,tag_name,tag_id from  t_product_tag  where is_index=1 and shop_id=0 order by tag_sort desc");
			if (list.size() > 0) {
				CacheUtils.set(key_index, list, RedisKeyConst.day);// 丢到缓存里面存储一天
			}
		}
		return list;
	}

	public  Map<String,Object> cache_getTagAndSubList(Integer  tag_id){
		Map<String,Object>    map=  CacheUtils.get(key_Tag_List+tag_id);
		if(map==null){
			map =new HashMap<String, Object>();
			map.put("info", ProductTag.dao.findById(tag_id));
			map.put("sub_list", ProductTag.dao.find("select tag_name,tag_id from  t_product_tag where pid=? order by tag_sort desc",tag_id));
			CacheUtils.set(key_Tag_List+tag_id,map,3600);//丢进缓存 1个小时
		}
		return map;
	}

	public void clear_cache() {
		if(getPid()==0){
			CacheUtils.del(key_Tag_List+getTagId());
		}else{
			CacheUtils.del(key_Tag_List+getPid());
		}
		CacheUtils.del(key_index);
	}

	/**
	 * fang 2016-06-15 根据shop_id 找到所有的分组
	 */
	public List<ProductTag> getTagsByShopId(String shop_id) {
		// String sql="select * from t_product_tag where shop_id=?";
		String sql = " select t.*,(select count(*) from t_product_tag_rel p where p.tag_id=t.tag_id   )as product_num from t_product_tag t where t.shop_id=?  order by t.tag_sort ";
		return dao.find(sql, shop_id);
	}

	/**
	 * 根据shop_id 找到所有的分组
	 */
	public List<ProductTag> getTagsBySystem() {
		String sql = " select t.*,(select count(1) from t_product_tag_rel  as rel where rel.tag_id=t.tag_id) as  product_num   from t_product_tag as t where t.shop_id='0' ORDER BY t.tag_sort desc";
		return dao.find(sql);
	}

	/**
	 * 获取前面五个热门的标签
	 * @return
	 */
	public List<ProductTag> getTopTags() {
		String sql = " select * from t_product_tag where shop_id='0' and    pid = 0 and state=1 ORDER BY tag_sort desc LIMIT 0,5";
		return dao.find(sql);
	}

	public List<ProductTag> getTopAll() {
		String sql = " select * from t_product_tag where shop_id='0' and    pid = 0 and state=1 ORDER BY tag_sort desc";
		return dao.find(sql);
	}
	/**
	 * 获取拼团榜里面的专题 要排除第一个
	 * @return
	 */
	public List<ProductTag> getRnakTagList() {
		String sql = " select * from t_product_tag where tag_id!=1 and  shop_id='0' and    pid = 0 and state=1 ORDER BY tag_sort desc ";
		return dao.find(sql);
	}


	/**
	 * 获取前面五个热门的标签
	 * @return
	 */
	public List<ProductTag> getTopTagsPage(int page) {
		String sql = " select * from t_product_tag where shop_id='0' and tag_type=1 ORDER BY tag_sort desc LIMIT ?,3";
		return dao.find(sql, page * 3);
	}

	/**
	 * 根据pid查询子标签
	 * @param pid
	 */
	public List<ProductTag> findTagByPid(Integer pid) {
		return dao.find(" select * from t_product_tag where pid=? ",pid);
	}
}
